package com.scau.databaseexam.controllers;

import com.scau.databaseexam.DAO.*;

import com.scau.databaseexam.beans.Item;
import com.scau.databaseexam.beans.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


import java.io.File;
import java.util.Collection;
import java.util.Date;
import java.util.UUID;

@Controller
public class PictureController {



    @Autowired
    ItemDao itemDao;



    @Value("${web.upload-path}")
    private String filePath;

    //查看图片
    @GetMapping("/emps")
    public String list(Model model,HttpSession session){
        User user = (User)session.getAttribute("loginUser");
        Collection<Item> items = itemDao.selectItems(user.getId());

        model.addAttribute("items",items);

        return "emp/list";
    }

    @GetMapping("/emp")
    public String toAddPage(){
        return "emp/add";
    }

    //添加
    @PostMapping("/item")
    public String addItem(@RequestParam("image") MultipartFile image, HttpServletRequest request, HttpSession session){
        User user = (User) session.getAttribute("loginUser");
        user.getId();

        //图片保存路劲
        String fileName = image.getOriginalFilename();
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        String id = UUID.randomUUID().toString().replace("-","");
        fileName =  id+suffix;

        String filePath = "/uploads/"+user.getId()+"/";
        System.out.println("FilePath:"+filePath+fileName);
        System.out.println("truepath:"+this.filePath+user.getId()+"/"+fileName);

        Item item = new Item(id, filePath+fileName,request.getParameter("script") , request.getParameter("name"), new Date(), user.getId());
        System.out.println(item.toString());
        //调用ItemDao，传入数据库
        itemDao.insertItem(item);

        File file = new File(this.filePath+user.getId()+"/"+fileName);
        try {
            image.transferTo(file);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "redirect:/emps";
    }


    //修改页面
    @GetMapping("/item/{id}")
    public String toItemEditPage(@PathVariable("id") String id, Model model,HttpSession session){

        User user = (User)session.getAttribute("loginUser");
        Item item = (Item)itemDao.selectItem(user.getId(),id);
        System.out.println(item);
        model.addAttribute("item",item);
        return "emp/update";
    }


    //修改
    //PutMapping和deleteMapping老出问题
    @PostMapping("/itemupdate")
    public  String updateItem(HttpServletRequest request, HttpSession session){

        System.out.println("跳转");
        String name = request.getParameter("name");
        String script = request.getParameter("script");

        String id = request.getParameter("id");
        User user = (User)session.getAttribute("loginUser");
        String uid = user.getId();
        itemDao.updateItem(uid,id,name,script,new Date());
        return "redirect:/emps";

    }


    @PostMapping("/item/{id}")
    public String deleteItem(@PathVariable("id") String id,HttpSession session){
        User user = (User)session.getAttribute("loginUser");
        Item item = (Item)itemDao.selectItem(user.getId(),id);

        String suffix = item.getImage().substring(item.getImage().lastIndexOf("."));
        System.out.println(this.filePath+user.getId()+"\\"+id+suffix);
        File file = new File(this.filePath+user.getId()+"\\"+id+suffix);
        file.delete();
        System.out.println("id "+id);
        itemDao.deleteItem(user.getId(),id);
        return "redirect:/emps";
    }

}
